package com.gxa.modules.property.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.property.dto.RepairToAdd;
import com.gxa.modules.property.dto.RepairToUpdate;
import com.gxa.modules.property.dto.UserRepairToUpdate;
import com.gxa.modules.property.entity.*;
import com.gxa.modules.property.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "报修管理接口")
@Slf4j
public class RepairController {

    @Autowired
    private RepairService repairService;

    @Autowired
    private RepairProjectService repairProjectService;

    @Autowired
    private HouseHoldTypeService houseHoldTypeService;

    @Autowired
    private RepairSecondItemService repairSecondItemService;


    @ApiOperation(value = "报修添加")
    @PostMapping("/repair/add")
    public Result add(@RequestBody RepairToAdd repairToAdd){
        if (repairToAdd != null){
            this.repairService.add(repairToAdd);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "报修删除")
    @DeleteMapping("/repair/delete")
    public Result delete(@RequestParam Integer id){
        this.repairService.delete(id);
        return new Result().ok();
    }

    @ApiOperation(value = "报修审核")
    @PutMapping("/repair/update")
    public Result update(@RequestBody RepairToUpdate repairToUpdate){
        if (repairToUpdate!=null){
            this.repairService.update(repairToUpdate);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "报修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前是第几页",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少条",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "repairStateId",value = "报修状态id",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value = "开始日期",dataType = "date"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value = "结束日期",dataType = "date"),
            @ApiImplicitParam(paramType = "query",name = "type",value = "住户类型",dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value = "小区编号",dataType = "int")


    })
    @GetMapping("/repair/list")
    public com.gxa.modules.property.utils.Result<List<Repair>> queryRepair(@RequestParam Map<String,Object> params){
        log.info("----params----{}----",params);
        AssertUtils.isBlank(String.valueOf(params),"字符串不能为空");
        com.gxa.modules.property.utils.Result<List<Repair>> r = new com.gxa.modules.property.utils.Result<>();
        try {
            PageInfo<Repair> pageInfo = repairService.queryRepair(params);
            r = com.gxa.modules.property.utils.Result.success(pageInfo.getList(),pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            com.gxa.modules.property.utils.Result.failed();
        }
        return r;
    }


    @ApiOperation(value = "报修状态下拉列表")
    @GetMapping("/repairState/list")
    public List<RepairState> getList(){
        return repairService.list();
    }

    @ApiOperation(value = "选择维修项目一级菜单")
    @GetMapping("/repairProject/list")
    public List<RepairProject> getRepairProjectList(){
        return repairProjectService.list();
    }

    @ApiOperation(value = "(查询条件)住户类型下拉列表")
    @GetMapping("/houseHoldType/list")
    public List<HouseHoldType> getHouseHoldTypeList(){
        return houseHoldTypeService.list();
    }

    @ApiOperation(value = "报修项目二级菜单家具接口")
    @GetMapping("/furniture/list")
    public List<RepairSecondItem> getFurnitureList(){
        QueryWrapper<RepairSecondItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("second_item_id",1001);
        List<RepairSecondItem> furnitureList = repairSecondItemService.list(queryWrapper);

        return furnitureList;
    }

    @ApiOperation(value = "报修项目二级菜单电路接口")
    @GetMapping("/electricCircuit/list")
    public List<RepairSecondItem> getElectricCircuitList(){
        QueryWrapper<RepairSecondItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("second_item_id",1002);
        List<RepairSecondItem> electricCircuitList = repairSecondItemService.list(queryWrapper);

        return electricCircuitList;
    }

    @ApiOperation(value = "报修项目二级菜单水暖接口")
    @GetMapping("/plumbing/list")
    public List<RepairSecondItem> getPlumbingList(){
        QueryWrapper<RepairSecondItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("second_item_id",1003);
        List<RepairSecondItem> plumbingList = repairSecondItemService.list(queryWrapper);

        return plumbingList;
    }

    @ApiOperation(value = "报修项目二级菜单门锁接口")
    @GetMapping("/doorLock/list")
    public List<RepairSecondItem> getDoorLockList(){
        QueryWrapper<RepairSecondItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("second_item_id",1004);
        List<RepairSecondItem> doorLockList = repairSecondItemService.list(queryWrapper);

        return doorLockList;
    }

    @ApiOperation(value = "报修项目二级菜单电器接口")
    @GetMapping("/appliance/list")
    public List<RepairSecondItem> getApplianceList(){
        QueryWrapper<RepairSecondItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("second_item_id",1005);
        List<RepairSecondItem> applianceList = repairSecondItemService.list(queryWrapper);

        return applianceList;
    }

    @ApiOperation(value = "小程序用户查看维修流程接口")
    @GetMapping("/repairProcess/list")
    public List<RepairState> getRepairProcess(@RequestParam Integer id){
        List<RepairState> repairProcessList = repairService.queryRepairState(id);
        return repairProcessList;
    }

    @ApiOperation(value = "小程序用户修改报修接口")
    @PutMapping
    public Result userRepairUpdate(@RequestBody UserRepairToUpdate userRepairToUpdate){
        if (userRepairToUpdate != null){
            this.repairService.userUpdate(userRepairToUpdate);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "小程序用户查询维修二级项目接口")
    @GetMapping("/secondItem/query")
    public List<SecondItem> querySecondItem(@RequestParam String secondItemType){
        List<SecondItem> secondItems = repairService.queryByName(secondItemType);
        return secondItems;
    }

}
